package flex.cc.complain.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;

import flex.cc.base.dto.LoginAuthDto;
import flex.cc.complain.service.ComplainService;
import flex.cc.complain.service.ComplainTrackService;
import flex.cc.complain.vo.ComplainVo;
import flex.cc.core.support.BaseController;
import flex.cc.order.manage.dto.order.OrderCustomerEntity;
import flex.cc.order.manage.dto.order.OrderMainEntity;
import flex.cc.util.FileDownloadUtil;
import flex.cc.wrapper.WrapMapper;
import flex.cc.wrapper.Wrapper;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping( value= "/complain")
public class ComplainController extends BaseController {
	@Autowired
	private ComplainService complainService;
	@Autowired
	private ComplainTrackService complainTrackService;
	
	@RequestMapping(value = "/list",method= RequestMethod.POST)
    @ApiOperation(value="投诉查询")
    public Wrapper<IPage<ComplainVo>> list(@RequestBody Map map){
    	IPage<ComplainVo> page = complainService.queryComplainList(map);
        return  WrapMapper.ok(page);
   }
	
	@RequestMapping(value = "/saveComplainInfo",method= RequestMethod.POST)
    @ApiOperation(value="投诉新增编辑")
    public Wrapper<Integer> saveComplainInfo(@RequestBody ComplainVo vo){
    	complainService.saveComplainInfo(vo);
        return  WrapMapper.ok();
   }
	
	@RequestMapping(value = "/querySearchAsync",method= RequestMethod.POST)
    @ApiOperation(value="服务单模糊搜索")
    public Wrapper<List<OrderMainEntity>> querySearchAsync(@RequestBody ComplainVo vo){
    	List<OrderMainEntity> querySearchAsync = complainService.querySearchAsync(vo.getFworder(),vo.getMobilephone());
        return  WrapMapper.ok(querySearchAsync);
   }
	
	@RequestMapping(value = "/querySearchAsyncMobilephone",method= RequestMethod.POST)
    @ApiOperation(value="联系方式模糊搜索")
    public Wrapper<List<OrderCustomerEntity>> querySearchAsyncMobilephone(@RequestBody ComplainVo vo){
    	List<OrderCustomerEntity> querySearchAsync = complainService.querySearchAsyncMobilephone(vo.getMobilephone());
        return  WrapMapper.ok(querySearchAsync);
   }
	@RequestMapping(value = "/queryTrackInfoByTsnumber",method= RequestMethod.POST)
    @ApiOperation(value="根据投诉单号查询操作轨迹")
    public Wrapper<List<ComplainVo>> queryTrackInfoByTsnumber(@RequestBody ComplainVo vo){
		List<ComplainVo> list = complainTrackService.queryTrackInfoByTsnumber(vo);
        return  WrapMapper.ok(list);
   }
	@RequestMapping(value = "/watchMessageInfo",method= RequestMethod.POST)
    @ApiOperation(value="操作轨迹查询")
    public Wrapper<IPage<ComplainVo>> watchMessageInfo(@RequestBody Map map){
		IPage<ComplainVo> queryComplainTrackByPage = complainTrackService.queryComplainTrackByPage(map);
        return  WrapMapper.ok(queryComplainTrackByPage);
   }
	/**
	   *     投诉导出
	 * */
    @ApiOperation(httpMethod = "POST",value="投诉导出")
    @PostMapping(value = "/downLoadComplainInfo")
    public void downLoadComplainInfo(HttpServletRequest request,HttpServletResponse response,@RequestBody Map map){
		LoginAuthDto loginAuthDto = getLoginAuthDto();
    	Map<String, Object> dataMap = complainService.downZgInfo(map,loginAuthDto);
    	FileDownloadUtil.downloadByByte(request, response, dataMap.get("templateName")+".xlsx",(byte[])dataMap.get("fileByte"));
   }
    /**
             * 服务单号创建投诉单号时，关联的投诉单状态查询校验
     * autor: wsp
     * date: 2021.10.25
     * */
    @RequestMapping(value = "/checkOrderStatusByFwOrder",method= RequestMethod.POST)
    @ApiOperation(value="根据服务单号查询投诉单状态")
    public Wrapper<String> checkOrderStatusByFwOrder(@RequestBody Map map){
    	String statusByFwOrder = complainService.checkOrderStatusByFwOrder(map);
        return  WrapMapper.ok(statusByFwOrder);
   }
    
    @RequestMapping(value = "/checkFwOrderStatus",method= RequestMethod.POST)
    @ApiOperation(value="根据服务单号查询服务单状态")
    public Wrapper<String> checkFwOrderStatus(@RequestBody Map map){
    	String status = this.complainService.checkFwOrderStatus(map);
        return  WrapMapper.ok(status);
   }
}
